package com.huage.courseplatform.satoken.handler;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotRoleException;
import com.huage.courseplatform.framework.enums.ResultCode;
import com.huage.courseplatform.framework.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)//提高执行顺序
public class LoginException {

    //未登录异常不获
    @ExceptionHandler(value = NotLoginException.class)
    public ResultUtils saTokenNotLoginException(NotLoginException e){
        //log.error("Exception:", e);
        return ResultUtils.error(ResultCode.UNAUTHORIZED.getCode(),ResultCode.UNAUTHORIZED.getMsg());
    }

    //无角色异常捕获
    @ExceptionHandler(value = NotRoleException.class)
    public ResultUtils saTokenNotRoleException(NotRoleException e){
        //log.error("Exception:", e);
        return ResultUtils.error(ResultCode.FORBIDDEN.getCode(),ResultCode.FORBIDDEN.getMsg());
    }
}
